﻿package com.schooltube.audio.microphone {
	
/*  -------------------------------------------------
    This file is part of the SchoolTube Live Platform
    Copyright © 2009 SchoolTube, LLC
    
    Licensed under the GNU General Public License v3
    http://www.gnu.org/licenses/gpl.html
    
    http://code.google.com/p/schooltubelive/
    ------------------------------------------------- */

/* Class from http://blog.skitsanos.com/2008/09/how-to-show-microphone-activity.html */

	import flash.display.Sprite;
	import flash.events.*;
	import flash.media.Microphone;
	import flash.system.Security;
	import flash.system.SecurityPanel;
	import flash.system.Capabilities;
	import flash.utils.Timer;
	import flash.display.Shape;
	import flash.display.Graphics;
	import flash.geom.*
	import flash.display.*
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.display.DisplayObject;
	
	public class ActivityMonitor extends Sprite {
	
		private var _mic_activity_indicator:Shape;
		private var _mic_activity_indicator_bkg:Shape;
		private var _mic_activity_indicator_gradient:Shape;
		private var _mic:Microphone;
		private var _micTimer:Timer;
		private var _width:int;
		private var _height:int;
		private var _colours:Array;
		
		public function ActivityMonitor(width:int, height:int, colours:Array, micid:int = 0) {
			_width = width;
			_height = height;
			_colours = colours;
			switch(Capabilities.os) {
				case 'Windows XP' :
				case 'Windows 2000' :
				case 'Windows NT' :
				case 'Windows 98/ME' :
				case 'Windows 95' :
				case 'Windows CE' :
				_mic = Microphone.getMicrophone(micid);
			break;
		
			default :
				_mic = Microphone.getMicrophone(-1);
			}
		
			_mic.setLoopBack(true);
			_mic.soundTransform = new SoundTransform(0, 0);
		
			if (_mic != null) {
				_mic.setUseEchoSuppression(true);
		
				_mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
				_mic.addEventListener(StatusEvent.STATUS, statusHandler);
		
				/* create the background*/
				_mic_activity_indicator_bkg = new Shape();
				_mic_activity_indicator_bkg.graphics.beginFill(0xCCCCCC);
				_mic_activity_indicator_bkg.graphics.drawRect(0, 0, _width, _height);
				_mic_activity_indicator_bkg.graphics.endFill();
				this.addChild(_mic_activity_indicator_bkg);
		
				/* create the maskee gradient*/
				var matr:Matrix = new Matrix();
				matr.createGradientBox(_width, _height, -Math.PI/2, 0, -(0.06*_height));
				_mic_activity_indicator_gradient = new Shape();
				_mic_activity_indicator_gradient.graphics.beginGradientFill(GradientType.LINEAR, _colours.slice(0, 3), [1, 1, 1], [150, 200, 245], matr, SpreadMethod.PAD);
				_mic_activity_indicator_gradient.graphics.drawRect(0, 0, _width, _height);
				_mic_activity_indicator_gradient.graphics.endFill();
				this.addChild(_mic_activity_indicator_gradient);
		
				/* create the masker*/
				_mic_activity_indicator = new Shape();
				this.addChild(_mic_activity_indicator);
		
				_mic_activity_indicator_gradient.mask = _mic_activity_indicator;
		
				_micTimer = new Timer(50, 0);
				_micTimer.addEventListener(TimerEvent.TIMER, showMicActivity);
				_micTimer.start();
			}
		}
		
		public function showMicActivity(e:TimerEvent):void {
			//trace(_mic.activityLevel);
			var h:int = (_mic.activityLevel > 0) ? _height*(_mic.activityLevel/100) : 0;
			var y:int = (_mic.activityLevel > 0) ? _height - h : _height;
			_mic_activity_indicator.graphics.clear();
			_mic_activity_indicator.graphics.beginFill(0x000000, 1);
			_mic_activity_indicator.graphics.drawRect(0, y, _width, h);
			_mic_activity_indicator.graphics.endFill();
		}
		
		public function updateMicSelection(id:int = 0):void {
			_mic = Microphone.getMicrophone(id);
		}
		
		private function activityHandler(event:ActivityEvent):void {
			//trace('activityHandler: ' + event);
		}
		
		private function statusHandler(event:StatusEvent):void {
			//trace('statusHandler: ' + event);
		}
	}
}